<?php
/**
 * 客户订单发货
 * (model)
 */
namespace App\Models\CustomerDelivery;


use App\Models\Basics\SysArea;
use App\Models\Basics\SysPlat;
use Illuminate\Database\Eloquent\Model;

class CustomerDelivery extends Model{

    protected $table = 'customer_delivery';

    protected $guarded  = [];

    /**
     * 发货状态
     * @var string[]
     */
    const STATUS_READY_NO      = 1; //待备货
    const STATUS_READY_YES     = 2; //已备货
    const STATUS_DELIVERY_NO   = 3; //待发货
    const STATUS_DELIVERY_PART = 4; //部分发货
    const STATUS_DELIVERY_YES  = 5; //已发货
    const STATUS_DELIVERY_ERROR  = 6; //异常
    const STATUS_DELIVERY_CANCEL  = 7; //取消
    public static $_status = [
        self::STATUS_READY_NO       => '待备货',
        self::STATUS_READY_YES      => '已备货',
        self::STATUS_DELIVERY_NO    => '待发货',
        self::STATUS_DELIVERY_PART  => '部分发货',
        self::STATUS_DELIVERY_YES   => '已发货',
        self::STATUS_DELIVERY_ERROR   => '异常',
        self::STATUS_DELIVERY_CANCEL   => '已取消',
    ];

    public static $status_abroad = [
        self::STATUS_DELIVERY_NO    => '待发货',
        self::STATUS_DELIVERY_YES   => '已发货',
        self::STATUS_DELIVERY_ERROR   => '异常',
        self::STATUS_DELIVERY_CANCEL   => '已取消',
    ];

    /**
     * 物流状态
     * @var string[]
     */
    const LOGISTICS_STATUS_NO   = 1; //待更新
    const LOGISTICS_STATUS_YES  = 2; //已更新
    public static $_logisticsStatus = [
        self::LOGISTICS_STATUS_NO  => '待更新',
        self::LOGISTICS_STATUS_YES => '已更新',
    ];

    /**
     * 发货方式
     * @var string[]
     */
    const DELIVERY_TYPE_FBA     = 1; //FBA(亚马逊仓)
    const DELIVERY_TYPE_ABROAD  = 2; //海外仓
    const DELIVERY_TYPE_CN      = 3; //国内仓
    public static $_deliveryType = [
        self::DELIVERY_TYPE_FBA     => 'FBA',
        self::DELIVERY_TYPE_ABROAD  => '海外仓',
        self::DELIVERY_TYPE_CN      => '国内仓',
    ];

    //乐天月报核算匹配各个发货方式的发货数量
    //增加发货方式也需要增加数据表lt_mr_payment_order的相关字段
    public static $_deliveryTypeMatchField = [
        self::DELIVERY_TYPE_ABROAD => 'os_quantity',
        self::DELIVERY_TYPE_FBA => 'fba_quantity',
        self::DELIVERY_TYPE_CN => 'cn_quantity',
    ];

    /**
     * 平台同步状态
     * @var string[]
     */
    const PLAT_SYNC_STATUS_NO  = 1; //待上传
    const PLAT_SYNC_STATUS_YES = 2; //已上传
    public static $_platSyncStatus = [
        self::PLAT_SYNC_STATUS_NO  => '待上传',
        self::PLAT_SYNC_STATUS_YES => '已上传',
    ];

    protected static function boot(){
        parent::boot();

        static::deleting(function($model) {
            if ($model->detail()) {
                $model->detail()->delete();
            }
            if ($model->receiver()) {
                $model->receiver()->delete();
            }
            if ($model->logistics()) {
                $model->logistics()->delete();
            }
        });
    }

    /**
     * 关联地区
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function area(){
        return $this->belongsTo(SysArea::class, 'area_id');
    }

    /**
     * 关联发货详情
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function detail(){
        return $this->hasMany(CustomerDeliveryDetail::class, 'delivery_no', 'delivery_no');
    }

    /**
     * 关联发货地址
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function receiver(){
        return $this->hasOne(CustomerDeliveryReceiver::class, 'delivery_no', 'delivery_no');
    }

    /**
     * 关联物流
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function logistics(){
        return $this->hasOne(CustomerDeliveryLogistics::class, 'delivery_no', 'delivery_no');
    }

    /**
     * 关联订单
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function order(){
        return $this->belongsTo(CustomerOrder::class, 'order_no', 'order_no');
    }

    /**
     * 关联平台
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function plat(){
        return $this->hasOne(SysPlat::class, 'plat_id', 'plat_id');
    }
    /**
     * 获取站外站点ID
     * @return int
     */
    public static function getSysAreaOutId(){
        return SysArea::getSysOutAreaIdByArea();
    }

    /**
     * 根据订单来源获取订单平台名称
     * @param $order_origin
     * @return string
     */
    public static function getOrderOriginName($order_origin){
        return !empty($order_origin) ? self::$_deliveryType[$order_origin] : '无';
    }

    /**
     * 获取发货单(未同步物流的发货单)
     * @param $platId
     * @param $deliveryType
     * @param array $deliveryNo
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public static function getCustomerDelivery($platId = null, $deliveryType = null, $deliveryNo = []){
        $query = self::query();
        if(!empty($platId)) $query->where('plat_id', $platId);
        if(!empty($deliveryType)) $query->where('delivery_type', $deliveryType);
        if(!empty($deliveryNo)) $query->whereIn('delivery_no', $deliveryNo);

        return $query->where('logistics_status', 1)->orderByDesc('id')->get();
    }

}
